home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Headers / STL Headers / map.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-01  |  5.1 KB  |  149 lines  |  [TEXT/MMCC]

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  */
  15.  
  16. #ifndef MAP_H
  17. #define MAP_H
  18.  
  19. #ifndef Allocator
  20. #define Allocator allocator
  21. #include <defalloc.h>
  22. #endif
  23.  
  24. #include <tree.h>
  25.  
  26. template <class Key, class T, class Compare>
  27. class map {
  28. public:
  29.  
  30. // typedefs:
  31.  
  32.     typedef Key key_type;
  33.     typedef pair</*const*/ Key, T> value_type;
  34.     typedef Compare key_compare;
  35.     
  36.     class value_compare
  37.         : public binary_function<value_type, value_type, bool> {
  38.     friend class map<Key, T, Compare>;
  39.     protected :
  40.         Compare comp;
  41.         value_compare(Compare c) : comp(c) {}
  42.     public:
  43.         bool operator()(const value_type& x, const value_type& y) const {
  44.             return comp(x.first, y.first);
  45.         }
  46.     };
  47.  
  48. private:
  49.     typedef rb_tree<key_type, value_type, 
  50.                     select1st<value_type, key_type>, key_compare> rep_type;
  51.     rep_type t;  // red-black tree representing map
  52. public:
  53.     typedef rep_type::pointer pointer;
  54.     typedef rep_type::reference reference;
  55.     typedef rep_type::const_reference const_reference;
  56.     typedef rep_type::iterator iterator;
  57.     typedef rep_type::const_iterator const_iterator;
  58.     typedef rep_type::reverse_iterator reverse_iterator;
  59.     typedef rep_type::const_reverse_iterator const_reverse_iterator;
  60.     typedef rep_type::size_type size_type;
  61.     typedef rep_type::difference_type difference_type;
  62.  
  63. // allocation/deallocation
  64.  
  65.     map(const Compare& comp = Compare()) : t(comp, false) {}
  66.     map(const value_type* first, const value_type* last, 
  67.         const Compare& comp = Compare()) : t(first, last, comp, false) {}
  68.     map(const map<Key, T, Compare>& x) : t(x.t, false) {}
  69.     map<Key, T, Compare>& operator=(const map<Key, T, Compare>& x) {
  70.         t = x.t;
  71.         return *this; 
  72.     }
  73.  
  74. // accessors:
  75.  
  76.     key_compare key_comp() const { return t.key_comp(); }
  77.     value_compare value_comp() const { return value_compare(t.key_comp()); }
  78.     iterator begin() { return t.begin(); }
  79.     const_iterator begin() const { return t.begin(); }
  80.     iterator end() { return t.end(); }
  81.     const_iterator end() const { return t.end(); }
  82.     reverse_iterator rbegin() { return t.rbegin(); }
  83.     const_reverse_iterator rbegin() const { return t.rbegin(); }
  84.     reverse_iterator rend() { return t.rend(); }
  85.     const_reverse_iterator rend() const { return t.rend(); }
  86.     bool empty() const { return t.empty(); }
  87.     size_type size() const { return t.size(); }
  88.     size_type max_size() const { return t.max_size(); }
  89.     Allocator<T>::reference operator[](const key_type& k) {
  90.         return (*((insert(value_type(k, T()))).first)).second;
  91.     }
  92.     void swap(map<Key, T, Compare>& x) { t.swap(x.t); }
  93.  
  94. // insert/erase
  95.  
  96.     typedef pair<iterator, bool> pair_iterator_bool; 
  97.     // typedef done to get around compiler bug
  98.     pair_iterator_bool insert(const value_type& x) { return t.insert(x); }
  99.     iterator insert(iterator position, const value_type& x) {
  100.         return t.insert(position, x);
  101.     }
  102.     void insert(const value_type* first, const value_type* last) {
  103.         t.insert(first, last);
  104.     }
  105.     void erase(iterator position) { t.erase(position); }
  106.     size_type erase(const key_type& x) { return t.erase(x); }
  107.     void erase(iterator first, iterator last) { t.erase(first, last); }
  108.  
  109. // map operations:
  110.  
  111.     iterator find(const key_type& x) { return t.find(x); }
  112.     const_iterator find(const key_type& x) const { return t.find(x); }
  113.     size_type count(const key_type& x) const { return t.count(x); }
  114.     iterator lower_bound(const key_type& x) {return t.lower_bound(x); }
  115.     const_iterator lower_bound(const key_type& x) const {
  116.         return t.lower_bound(x); 
  117.     }
  118.     iterator upper_bound(const key_type& x) {return t.upper_bound(x); }
  119.     const_iterator upper_bound(const key_type& x) const {
  120.         return t.upper_bound(x); 
  121.     }
  122.     typedef pair<iterator, iterator> pair_iterator_iterator; 
  123.     // typedef done to get around compiler bug
  124.     pair_iterator_iterator equal_range(const key_type& x) {
  125.         return t.equal_range(x);
  126.     }
  127.     typedef pair<const_iterator, const_iterator> pair_citerator_citerator; 
  128.     // typedef done to get around compiler bug
  129.     pair_citerator_citerator equal_range(const key_type& x) const {
  130.         return t.equal_range(x);
  131.     }
  132. };
  133.  
  134. template <class Key, class T, class Compare>
  135. inline bool operator==(const map<Key, T, Compare>& x, 
  136.                        const map<Key, T, Compare>& y) {
  137.     return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
  138. }
  139.  
  140. template <class Key, class T, class Compare>
  141. inline bool operator<(const map<Key, T, Compare>& x, 
  142.                       const map<Key, T, Compare>& y) {
  143.     return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  144. }
  145.  
  146. #undef Allocator
  147.  
  148. #endif
  149.